﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Manager;
using Model;
using ExtAspNet;
namespace HealthSystem
{
    public partial class SchoolAdmin : BasePage
    {
        HW_School_UserManager manager = new HW_School_UserManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                searchbt.OnClientTriggerClick = Window1.GetSaveStateReference(districtid.ClientID,searchbt.ClientID)
                    + Window1.GetShowReference("./ChooseDistrict.aspx");
                Initialize();
                BindGrid();
                if (CustomerProfile.RoleId == 1003 || CustomerProfile.RoleId == 1012)
                {
                    districtid.Text = CustomerProfile.AdminUser.DistrictCode;
                    searchbt.Text = CustomerProfile.AdminUser.hW_District.Name;
                    searchbt.Readonly = true;
                }
            }
        }
        public void BindGrid()
        {
            int count = 0;
            try
            {
                 DataTable list = new DataTable();
                if (CustomerProfile.RoleId.ToString().StartsWith("1"))
                {
                    if (action.Text == "search")
                    {
                        list = manager.Search(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.DistrictId, searchbt2.Text.Trim());
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(districtid.Text))
                           list = manager.GetSchoolUser(Gird1.PageIndex, Gird1.PageSize, out count, districtid.Text);
                        else
                            list = manager.GetSchoolUser(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.DistrictId);
                    }
                }
                else if (CustomerProfile.RoleId == 2000)
                {
                    list = manager.GetByOrgCode(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.OrgUser.OrgCode);
                }
                else
                {
                    list = manager.GetBySchoolCode(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.SchoolUser.SchoollCode);
                }
                Gird1.RecordCount = count;
                Gird1.DataSource = list;
                Gird1.DataBind();
            }
            catch
            {
                ExtAspNet.Alert.Show("数据出错");
            }

        }
        public void Grid1_DoubleClick(object sender,ExtAspNet.GridRowClickEventArgs e)
        {

        }
        public void Window1_Close(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(districtid.Text))
            {
                Gird1.PageIndex = 0;
                BindGrid();
            }
        }
        public void PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Gird1.PageIndex=e.NewPageIndex;
            BindGrid();
        }
        protected void DistrictClick(object sender, EventArgs e)
        {

        }
        protected void SearchClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchbt2.Text))
            {
                ExtAspNet.Alert.Show("请填写好搜索条件!");
                return;
            }
            Gird1.PageIndex = 0;
            action.Text = "search";
            BindGrid();
        }
        public void Search()
        {
            int count = 0;
            DataTable list = manager.Search(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.DistrictId, searchbt2.Text.Trim());
            Gird1.RecordCount = count;
            Gird1.DataSource = list;
            Gird1.DataBind();
        }
        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            try
            {
                string username = Gird1.Rows[e.RowIndex].DataKeys[0].ToString();
                //if (e.CommandName == "delete")
                //{
                //    manager.DeleteById(username);
                //    BindGrid();

                //}
                if (e.CommandName == "reset")
                {
                    //重置密码
                    HW_School_UserManager schoolUserManager = new HW_School_UserManager();
                    string pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    int n = schoolUserManager.ResetPassWord(username, pass);
                    if (n == 1)
                        Alert.Show("密码重置成功");
                    else
                        Alert.Show("密码出错!");
                    //BindGrid();
                }
            }
            catch
            {
                Alert.Show("操作失败!");
            }
        }
        public void Initialize()
        {
            if (!CustomerProfile.RoleId.ToString().StartsWith("1"))
            {
                lable1.Visible = false;
                searchbt.Visible = false;
                searchlable.Visible = false;
                searchbt2.Visible = false;

            }
        }
    }
}